package com.mmall.concurrence.test;

public class ThreadDemo1 implements Runnable {

    private static int count = 100;


    @Override
    public void run() {
        while (count > 0) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            sale();
        }
    }

    public void sale() {

        synchronized (this) {

//        if(count>0){
            System.out.println(Thread.currentThread().getName() + ",出售第" + (100 - count + 1) + "---张票");
            count--;

//        }
        }

    }


    public static void main(String[] args) {
        ThreadDemo1 threadDemo1 = new ThreadDemo1();
        Thread t1 = new Thread(threadDemo1, "一号");
        Thread t2 = new Thread(threadDemo1, "二号");
        t1.start();
        t2.start();
    }
}
